import { ActionReducerMapBuilder, createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';
import type { Department, DepartmentTree, MapSimple, PickDepartment, Simple } from '@/type/DepartmentType';
import { addDepartmentsAPI, deleteDepartmentsAPI, fetchDepartmentsAPI, fetchSimpleAPI, findDepartmentsAPI, updateDepartmentsAPI } from '@/api/departmentAPI';
import { transitionToTreeData } from '@/utils';


type InitialState = {
  departments: DepartmentTree[],
  simples: MapSimple[],
  departmentDetail: Department,

}

const initialState: InitialState = {
  departments: [],
  simples: [],
  departmentDetail: {} as Department,
};

/**
 * @description 获取部门列表的异步回调函数
 * */
export const fetchDepartmentsList: any = createAsyncThunk('fetchDepartment', async (_, { rejectWithValue }) => {
  try {
    const { data }: { data: Department[] } = await fetchDepartmentsAPI();
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 删除部门列表的异步回调函数
 * */
export const deleteDepartments: any = createAsyncThunk('deleteDepartments', async (id: string, { rejectWithValue }) => {
  try {
    const { message }: { message: string } = await deleteDepartmentsAPI(id);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }

});

/**
 * @description 添加部门的异步回调函数
 * */
export const addDepartments: any = createAsyncThunk('addDepartments', async (formData: PickDepartment, { rejectWithValue }) => {
  try {
    const { message }: { message: string } = await addDepartmentsAPI(formData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 获取部门负责人的异步回调函数
 * */
export const fetchSimple: any = createAsyncThunk('fetchSimple', async (_, { rejectWithValue }) => {

  try {
    const { data }: { data: Simple[] } = await fetchSimpleAPI();
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 查询-部门详情的异步回调函数
 * */
export const findDepartments: any = createAsyncThunk('findDepartments', async (id: string, { rejectWithValue }) => {
  try {
    const { data }: { data: Department } = await findDepartmentsAPI(id);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 修改-部门详情的异步回调函数
 * */
export const updateDepartments: any = createAsyncThunk('updateDepartments', async (formData: Department, { rejectWithValue }) => {
  try {
    const { message }: { message: string } = await updateDepartmentsAPI(formData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

const departmentStore = createSlice({
  name: 'departmentStore',
  initialState,
  reducers: {},
  extraReducers(builder: ActionReducerMapBuilder<InitialState>): void {
    builder.addCase(fetchDepartmentsList.fulfilled, (state: InitialState, action: PayloadAction<Department[]>): void => {
      const { payload }: { payload: Department[] } = action;
      state.departments = transitionToTreeData(payload, 0);
    });
    builder.addCase(fetchSimple.fulfilled, (state: InitialState, action: PayloadAction<Simple[]>): void => {
      const { payload }: { payload: Simple[] } = action;
      state.simples = payload.map((item: Simple): MapSimple => ({ value: item.id, label: item.username }));
    });
    builder.addCase(findDepartments.fulfilled, (state, action: PayloadAction<Department>): void => {
      const { payload }: { payload: Department } = action;
      state.departmentDetail = payload;
    });
  },
});

export default departmentStore.reducer;
